<?php

declare(strict_types=1);

namespace Dreamcat\Class2Array\Cases;

use Dreamcat\Class2Array\DemoClass\Child;
use Dreamcat\Class2Array\Exception\EncodeError;
use Dreamcat\Class2Array\Impl\Class2Json;
use PHPUnit\Framework\TestCase;

/**
 * 测试解码
 * @author vijay
 */
class JsonDecodeTest extends TestCase
{
    public function testNormal()
    {
        $e = new Child(pv3: "pv3", cf1: "abc");
        $s = '{"cf1":"abc","sharePrivate":"pv2::child","share_public":"pv3::child"}';
        $o = new Class2Json();
        $result = $o->jsonDecode($s, Child::class);
        self::assertEquals($e, $result);
    }

    public function testInvalidJsonStr()
    {
        $this->expectException(EncodeError::class);
        $this->expectExceptionMessage("Syntax error");
        $s = '{';
        $o = new Class2Json();
        $o->jsonDecode($s, Child::class);
    }

    public function testInvalidType()
    {
        $this->expectException(EncodeError::class);
        $this->expectExceptionMessage("指定的输出类型 s 不存在");
        $s = '{}';
        $o = new Class2Json();
        $o->jsonDecode($s, "s");
    }
}

# end of file
